package com.sbl.bajw.pc.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.mapper.CarStaticDao;
import com.sbl.bajw.pc.model.AttFileModel;
import com.sbl.bajw.pc.model.CarStaticModel;
import com.sbl.bajw.util.ExecptionUtil;
import com.sbl.bajw.util.LogUtil;

@Service
public class CarStaticService {

	@Resource
	private CarStaticDao dao;
	
	@Resource
	private MyBatisDao gridDao;
	
	@Resource
	private AttFileService attfileService;
	
	static Logger log;
	
	static {
		log = LogUtil.getLogger("Car");
	}
	
	@Transactional(readOnly = false)
	public void insert(CarStaticModel car) throws Exception{
		log.info(car.getCreater() + "开始入库，属性" + car.toString() + ",经度：" + RequestUnit.getString("lon"));
		if(car.getLon() < 100){
			log.info(car.getCreater() + "经纬度不合法");
			throw new MessageException("经纬度不合法", "3");
		}
		if(car.getUuid() != null && dao.getByUUID(car.getUuid()) > 0){
			log.info(car.getCreater() + "重复提交");
			throw new MessageException("重复提交", "3");
		}
		try {
			uploadFile(car);
			dao.insert(car);
			log.info(car.getCreater() + "数据入库成功！");
		} catch (Exception e) {
			String msg = ExecptionUtil.getStackTrace(e);
			log.info("录入异常："+msg);
			throw e;
		}
	}
	
	public void update(CarStaticModel car) throws Exception{
		dao.update(car);
	}
	
	public String getList(HashMap<String, Object> parm){
		String list = gridDao.selectPage(CarStaticDao.class, parm, "getList");
		return list;
	}
	
	public List<CarStaticModel> downloadList(HashMap<String, Object> parm){
		List<CarStaticModel> list = dao.getList(parm);
		return list;
	}
	
	public CarStaticModel getModel(String id){
		CarStaticModel car = dao.getModel(id);
		return car;
	}
	
	public List<HashMap<String, String>> getPics(String id){
		List<HashMap<String, String>> pics = dao.getPics(id);
		return pics;
	}
	
	public List<HashMap<String, String>> getListApp(HashMap<String, String> parm){
		List<HashMap<String, String>> list = dao.getListApp(parm);
		return list;
	}
	
	public int getCountApp(HashMap<String, String> parm){
		int num = dao.getCountApp(parm);
		return num;
	}
	
	public List<CarStaticModel> getCarByCode(String carCode) throws Exception{
		List<CarStaticModel> list = dao.getCarByCode(carCode);
		return list;
	}
	
	public void uploadFile(CarStaticModel	car) throws Exception {
		String id = dao.getCarId();

		if (id == null || id.equals("")) {
			throw new MessageException("ID不能为空", "2");
		}

		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssS");
		
		FileItem item = null;
		int i = 1;
		
		if((item = RequestUnit.getFileItem("img1")) == null){
			throw new MessageException("图片不能为空", "3");
		}
		
		while((item = RequestUnit.getFileItem("img" + i)) != null){
			String dataStr = sf.format(Calendar.getInstance().getTime());
			
			AttFileModel filemodel = new AttFileModel();
			filemodel.setBusinessType("7");
			filemodel.setFileDir("car_static/image");
			filemodel.setFileName(dataStr + i +  "_img_" + id);
			filemodel.setGroupid(id);
			filemodel.setItem(item);
			filemodel.setUserid(car.getCreater());
			filemodel.setFiletype("img");
			AttFileModel model = attfileService.insert(filemodel);
			log.info(car.getCreater() + "图片上传成功" + model.getSmallimgurl());
			car.setImgurl(model.getSmallimgurl());
			i++;
		}
		
		
		FileItem record = RequestUnit.getFileItem("voice");
		if (record != null) {
			String dataStr = sf.format(Calendar.getInstance().getTime());
			
			AttFileModel filemodel = new AttFileModel();
			filemodel.setBusinessType("7");
			filemodel.setFileDir("car_static/voice");
			filemodel.setFileName(dataStr + "_record_" + id);
			filemodel.setGroupid(id);
			filemodel.setItem(record);
			filemodel.setUserid(car.getCreater());
			filemodel.setFiletype("record");
			attfileService.insert(filemodel);
			log.info(car.getCreater() + "录音上传成功");
		} else {
			log.info(SessionUnit.getSessionId() + "没有录音数据");
		}
		
		car.setCarId(id);
	}
}
